/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ids.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author isuru.j
 */
public class DataHandler {
    
    private static ArrayList<String> filePaths;
    
    public static ArrayList<String> getFilePaths()
    {
         //return if url list is already full
        if (filePaths!=null && filePaths.size()>0 ){
            
            return filePaths;
        }
        
        filePaths = new ArrayList<String>();
        //get the URLs from the DB
            // JDBC driver name and database URL
   final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   final String DB_URL = "jdbc:mysql://localhost/intelidis";

   //  Database credentials
   final String USER = "root";
   final String PASS = "";
   
   Connection conn = null;
   Statement stmt = null;
   
   try{
     
      Class.forName("com.mysql.jdbc.Driver");

      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      stmt = conn.createStatement();

      String sql = "SELECT * FROM intelidis.textfilestab";
      ResultSet rs = stmt.executeQuery(sql);
      //STEP 5: Extract data from result set
      while(rs.next()){
         filePaths.add(rs.getString("FilePath"));
      }
      rs.close();
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(stmt!=null)
            conn.close();
      }catch(SQLException se){
      }// do nothing
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
        return filePaths;
    }
}
